﻿using ACGN.Application.Products;
using ACGN.Application.Products.Dtos;
using ACGN.Core.Models;
using ACGN.Core.Repository;
using ACGN.Mvc.Controllers;
using ACGN.MVC.Models.Productss;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ACGN.MVC.Areas.Admin.Controllers
{
    [Area("Admin")]
    public class ProductController : BaseController
    {
        private readonly ILogger<ProductController> _logger;
        private readonly IRepository<Product, int> _repository;
        private readonly IProductService _productService;
        private readonly IMapper _mapper;
        private readonly IRepository<ProType, int> _proTypeRepository;

        public ProductController(ILogger<ProductController> logger,
            IRepository<Product, int> repository,
            IProductService productService,
            IMapper mapper,
            IRepository<ProType, int> proTypeRepository
            )
        {
            _logger = logger;
            _repository = repository;
            _productService = productService;
            _mapper = mapper;
            _proTypeRepository = proTypeRepository;
        }
        public async Task<IActionResult> Index()
        {
            ViewBag.ProTypeList = new MultiSelectList(await _proTypeRepository.GetAllListAsync(), "TId", "TName");
            return View();
        }
        public async Task<IActionResult> List(ProductInputDto input)
        {
            var product = await _productService.GetPageResult(input);
            return Json(ListResult(_mapper.Map<List<ProductViewModel>>(product.Data), product.TotalCount));
        }
        [HttpGet]
        public async Task<IActionResult> Add()
        {
            ViewBag.ProTypeList = new MultiSelectList(await _proTypeRepository.GetAllListAsync(), "TId", "TName");
            return View();
        }
        [HttpPost]
        public async Task<ActionResult<Product>> AddPro(Product product)
        {
            product.CreateTime = DateTime.Now;
            await _repository.InsertAsync(product);
            return CreatedAtAction(nameof(List), new { id = product.PId }, product);
        }
        [HttpGet]
        public async Task<IActionResult> Edit(int pId)
        {
            Product product = await _repository.FirstOrDefaultAsync(s => s.PId == pId);
            ViewBag.ProTypeList = new MultiSelectList(await _proTypeRepository.GetAllListAsync(), "TId", "TName");
            return View(_mapper.Map<ProductViewModel>(product));
        }
        [HttpPost]
        public async Task<ActionResult> EditPro(int pId, Product product)
        {
            if (pId != product.PId)
            {
                return Json(AjaxResult(false));
            }
            product.CreateTime = DateTime.Now;
            await _repository.UpdateAsync(product);
            return Json(AjaxResult(true));
        }
        public async Task<ActionResult> Delete(int PId, Product product)
        {
            if (PId != product.PId)
            {
                return Json(AjaxResult(false));
            }
            await _repository.DeleteAsync(product);
            return Json(AjaxResult(true));
        }
    }
}
